package com.water.expert.extraction.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.water.expert.extraction.exception.CustomValidationException;
import com.water.expert.extraction.repository.Expert;
import com.water.expert.extraction.repository.Extraction;
import com.water.expert.extraction.repository.ExtractionRepository;
import com.water.expert.extraction.repository.Profession;

@Service
public class ExtractionService {
	@Autowired
	private ExpertService expertService;
	@Autowired
	ExtractionResultService extractionResultService;
	@Autowired
	private ExtractionRepository extractionRepository;

	public Extraction createExtractionAndGenExtractionResult(Extraction extraction) {
		Profession currentProfession = extraction.getCurrentProfession();
		List<Expert> experts = expertService.getExpertByCurrentProfession(currentProfession);
		int selectionCount = extraction.getSelectionCount();
		if(selectionCount< experts.size()) {
			throw new CustomValidationException("没有足够的专家供抽取，现有符合专家的人数为"+experts.size());
		}
		
		// 保存数据
		Extraction savedExtraction = extractionRepository.save(extraction);
		// 生成抽取结果
		extractionResultService.genExtractionResultByExtraction(extraction);
		
		return savedExtraction;
	}
	
	public Extraction getExtractionById(Long id) {
		return extractionRepository.findById(id).orElseThrow(() -> new CustomValidationException("Extraction不存在: " + id));
	}
}
